<?php
namespace Admin\Controller;

class UserController extends CommonController {


    public function index(){
        $user = M('user');
        import('ORG.Util.Page');
        $count = $user->table('dota2_user_info ui, dota2_user u')->where('ui.ui_id=u.u_id')->count();
        $page = new \Think\Page($count,3);
        $show = $page->show();
        $data = $user->table('dota2_user_info ui, dota2_user u')->where('ui.ui_id=u.u_id')->order('u_id')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('data',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function edit()
    {   
        // $data =M('user')->field('u_id')->where('email="'.$_POST['email'].'"')->find()['u_id'];
        $data = M('user_info')->create();
        $res = M('user_info')->where("ui_id=".M('user')->field('u_id')->where('email="'.$_POST['email'].'"')->find()['u_id'])->save($data);
        if ($res == 1) {
            echo '修改成功';
        }else{
            echo '修改失败';
        }
    }

    public function del()
    {   
        $id = M('user')->field('u_id')->where('email="'.$_GET['email'].'"')->find()['u_id'];
        $delu = M('user')->where('email="'.$_GET['email'].'"')->delete();
        $delui = M('user_info')->where("ui_id=".$id)->delete();
        if ($delui) {
            echo '删除成功';
        }else{
            echo '删除失败';
        }
    }

    public function add()
    {   
        //得到自定的模型类
        $user = D('user');
        if (!$user->create()) {
            //如果创建数据失败,表示自动验证没有通过,就输出错误信息,并跳转
            echo $user->getError();
        } else {
            //通过验证 就进行添加 并返回自增ID
            if ($user->add() > 0) {
                $id = M('user')->field('u_id')->where('email="'.$_POST['email'].'"')->find()['u_id'];
                $data = M('user_info')->create();
                $data['ui_id'] = $id;
                M('user_info')->add($data);
                echo '添加成功';
            } else {
                echo '添加失败';
            }
        }
        // $data =M('user')->field('u_id')->where('email="'.$_POST['email'].'"')->find()['u_id'];
        // $data = M('user_info')->create();
        // $res = M('user_info')->where("ui_id=".M('user')->field('u_id')->where('email="'.$_POST['email'].'"')->find()['u_id'])->save($data);
        // if ($res == 1) {
        //     echo '修改成功';
        // }else{
        //     echo '修改失败';
        // }
    }

}